package com.ruoyi.project.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.project.system.domain.OrderDetails;
import com.ruoyi.project.system.mapper.OrderDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.OrderMapper;
import com.ruoyi.project.system.domain.Order;
import com.ruoyi.project.system.service.IOrderService;

/**
 * 订单Service业务层处理
 *
 * @author tsxuwj
 * @date 2020-06-20
 */
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    /**
     * 查询订单
     *
     * @param id 订单ID
     * @return 订单
     */
    @Override
    public Order selectOrderById(Long id) {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param order 订单
     * @return 订单
     */
    @Override
    public List<Order> selectOrderList(Order order) {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    public int insertOrder(Order order) {
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    public int updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单ID
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Long[] ids) {
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单ID
     * @return 结果
     */
    @Override
    public int deleteOrderById(Long id) {
        return orderMapper.deleteOrderById(id);
    }

    /**
     * 新增订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    public int insertOrderTSXUWJ(Order order) {
        BigDecimal constructionPrice = order.getConstructionPrice().multiply(new BigDecimal(String.valueOf(order.getLabor() / 100.0)));
        order.setTaxation(constructionPrice);
        order.setPrice(order.getConstructionPrice().add(constructionPrice));
        order.setPriceCost(order.getConstructionPriceCost().add(constructionPrice));
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    public int updateOrderTSXUWJ(Order order) {
        BigDecimal equipmentPrice = order.getEquipmentPrice().multiply(new BigDecimal(String.valueOf(order.getProduct() / 100.0)));
        BigDecimal constructionPrice = order.getConstructionPrice().multiply(new BigDecimal(String.valueOf(order.getLabor() / 100.0)));
        order.setTaxation(equipmentPrice.add(constructionPrice));
        order.setPrice(order.getEquipmentPrice().add(order.getConstructionPrice().add(order.getTaxation())));
        order.setPriceCost(order.getEquipmentPriceCost().add(order.getConstructionPriceCost().add(order.getTaxation())));
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单ID
     * @return 结果
     */
    @Override
    public int deleteOrderByIdTSXUWJ(Long[] ids) {
        for (Long id : ids) {
            delectOrderAndOrderDetailsTSXUWJ(id);
        }
        return 1;
    }

    private int delectOrderAndOrderDetailsTSXUWJ(Long id){
        OrderDetails orderDetails = new OrderDetails();
        orderDetails.setOrderId(id);
        List<OrderDetails> orderDetailsList = orderDetailsMapper.selectOrderDetailsList(orderDetails);
        if (orderDetailsList.size() > 0) {
            Long[] ids = new Long[orderDetailsList.size()];
            for (int i = 0; i < orderDetailsList.size(); i++) {
                ids[i] = orderDetailsList.get(i).getId();
            }
            orderDetailsMapper.deleteOrderDetailsByIds(ids);
        }
        return orderMapper.deleteOrderById(id);
    }
}
